package com.mixsmart.web.plugins.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mixsmart.commonweb.plugins.IPlugin;
import com.mixsmart.commonweb.plugins.PluginFactory;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.utils.StringUtils;
import com.mixsmart.web.controller.BaseWebController;
import com.mixsmart.web.plugins.ZTreePlugin;
import com.mixsmart.web.plugins.bean.ZTreeData;
import com.mixsmart.web.plugins.ztree.UrlResOpFlag2zTree;
import com.mixsmart.web.plugins.ztree.UrlResView2zTree;

/**
 * ZTree插件控制器
 * @author lmq
 * @version 1.0
 * @since 1.0
 * <br />
 * 2016年7月3日
 */
@Controller
@RequestMapping("/ztree")
public class ZTreeController extends BaseWebController {

	@Autowired
	@Qualifier("ztree")
	private IPlugin ztree;
	
	/**
	 * 通过资源名称查询获取zTree插件的数据结构
	 * @param request HttpServletRequest对象
	 * @param resName 资源名称（SQL名称）
	 * @return 返回MixResponse<Object>对象，以JSON格式方式返回给前台页面；
	 * <p>如果成功查询到数据；则getResult()方法返回值为“1”</p>
	 */
	@RequestMapping("/query/{resName}.do")
	@ResponseBody
	public MixResponse<ZTreeData> query(HttpServletRequest request,@PathVariable String resName) {
		MixResponse<ZTreeData> mixResp = new MixResponse<ZTreeData>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = getRequestParamMap(request);
			mixResp = PluginFactory.getPluginDatas(ztree, resName, params);
			params = null;
		}
		return mixResp;
	}
	
	/**
	 * 通过资源名称异步查询获取zTree插件的数据结构
	 * @param request HttpServletRequest对象
	 * @param resName 资源名称（SQL名称）
	 * @return 返回MixResponse<Object>对象，以JSON格式方式返回给前台页面；
	 * <p>如果成功查询到数据；则getResult()方法返回值为“1”</p>
	 */
	@RequestMapping("/async/query/{resName}.do")
	@ResponseBody
	public MixResponse<ZTreeData> asyncQuery(HttpServletRequest request,@PathVariable String resName) {
		MixResponse<ZTreeData> mixResp = new MixResponse<ZTreeData>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = getRequestParamMap(request);
			params.put("isAsync", true);
			Object obj = params.get("id");
			if(obj.getClass().isArray()) {
				Object[] objs = (Object[])obj;
				params.put("id",objs[objs.length-1]);
			}
			mixResp = PluginFactory.getPluginDatas(ztree, resName, params);
			params = null;
		}
		return mixResp;
	}
	
	/**
	 * 角色资源配置
	 * @param request HttpServletRequest对象
	 * @param resName 资源名称（SQL名称）
	 * @return 返回MixResponse<Object>对象，以JSON格式方式返回给前台页面；
	 * <p>如果成功查询到数据；则getResult()方法返回值为“1”</p>
	 */
	@RequestMapping("/roleResConfig/{resName}.do")
	@ResponseBody
	public MixResponse<ZTreeData> roleResConfig(HttpServletRequest request,@PathVariable String resName) {
		MixResponse<ZTreeData> mixResp = new MixResponse<ZTreeData>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = getRequestParamMap(request);
			mixResp = PluginFactory.getPluginDatas(new ZTreePlugin(new UrlResOpFlag2zTree()), resName, params);
			params = null;
		}
		return mixResp;
	}
	
	/**
	 * 查看角色资源
	 * @param request HttpServletRequest对象
	 * @param resName 资源名称（SQL名称）
	 * @return 返回MixResponse<Object>对象，以JSON格式方式返回给前台页面；
	 * <p>如果成功查询到数据；则getResult()方法返回值为“1”</p>
	 */
	@RequestMapping("/roleResView/{resName}.do")
	@ResponseBody
	public MixResponse<ZTreeData> roleResView(HttpServletRequest request,@PathVariable String resName) {
		MixResponse<ZTreeData> mixResp = new MixResponse<ZTreeData>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = getRequestParamMap(request);
			mixResp = PluginFactory.getPluginDatas(new ZTreePlugin(new UrlResView2zTree()), resName, params);
			params = null;
		}
		return mixResp;
	}
	
}
